<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../layuimini/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../layuimini/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">-
    <div class="layuimini-main">

        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
            <legend>个人信息</legend>
        </fieldset>
        <form class="layui-form layui-form-pane" action="" lay-filter="example">
            <div class="layui-form-item">
                <label class="layui-form-label">账号</label>
                <div class="layui-input-inline">
                    <input type="text" name="account" autocomplete="off" placeholder="请输入标题" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">用户名</label>
                <div class="layui-input-inline">
                    <input type="text" name="uname" lay-verify="required" placeholder="请输入" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">密码</label>
                <div class="layui-input-inline">
                    <input type="text" name="password" placeholder="请输入密码" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">年龄</label>
                <div class="layui-input-inline">
                    <input type="text" name="age" lay-verify="required" placeholder="请输入" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">性别</label>
                <div class="layui-input-block">
                    <input type="radio" name="sex" value="男" title="男" checked="">
                    <input type="radio" name="sex" value="女" title="女">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">联系电话</label>
                <div class="layui-input-inline">
                    <input type="text" name="telephone" lay-verify="required" placeholder="请输入" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">地址</label>
                <div class="layui-input-inline">
                    <input type="text" name="address" lay-verify="required" placeholder="请输入" autocomplete="off"
                           class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <button class="layui-btn" lay-submit="" lay-filter="demo2" type="button">修改信息</button>
            </div>

        </form>
        </form>
    </div>
</div>

<script src="../layuimini/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    layui.use(['form', 'layedit', 'laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            layer = layui.layer,
            layedit = layui.layedit,
            laydate = layui.laydate;

        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });

        //自定义验证规则
        form.verify({
            title: function (value) {
                if (value.length < 5) {
                    return '标题至少得5个字符啊';
                }
            }
            , pass: [
                /^[\S]{6,12}$/
                , '密码必须6到12位，且不能出现空格'
            ]
            , content: function (value) {
                layedit.sync(editIndex);
            }
        });

        //监听指定开关
        form.on('switch(switchTest)', function (data) {
            layer.msg('开关checked：' + (this.checked ? 'true' : 'false'), {
                offset: '6px'
            });
            layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是ON|OFF', data.othis)
        });

        //监听提交
        form.on('submit(demo2)', function (data) {
            var id = session.id;
            console.log(id)

            $.ajax({
                url: "http://localhost:8080/order/userUpdateServlet",
                type: "POST",
                async: false,
                dataType: "json",
                data: {
                    "id": id,
                    "account": data.field.account,
                    "uname": data.field.uname,
                    "password": data.field.password,
                    "sex": data.field.sex,
                    "age": data.field.age,
                    "telephone": data.field.telephone,
                    "address": data.field.address
                },
                success: (res) => {
                    if (res.code === 200) {
                        layer.msg('用户信息修改成功');
                        form.val('example', {
                            "account": data.field.account,
                            "uname": data.field.uname,
                            "password": data.field.password,
                            "sex": data.field.sex,
                            "age": data.field.age,
                            "telephone": data.field.telephone,
                            "address": data.field.address
                        })
                    } else {
                        console.log("更改失败");
                    }
                },
                error: (err) => {
                    window.location = '404.html';
                }
            })
            return false;
        });

        //获取session
        var layuiSession = sessionStorage.getItem("user");
        var session = JSON.parse(layuiSession);
        var account = session.account;

        $.ajax({
            url: "http://localhost:8080/order/userInfoServlet",
            type: "POST",
            async: false,
            dataType: "json",
            data: {
                "account": account,
            },
            success: (res) => {
                if (res.code === 200) {
                    //表单初始赋值
                    /*session存储*/
                    layui.sessionData('user', {
                        key: 'id',
                        value: res.data.id
                    });
                    form.val('example', {
                        "account": res.data.account,
                        "uname": res.data.uname,
                        "password": res.data.password,
                        "sex": res.data.sex,
                        "age": res.data.age,
                        "telephone": res.data.telephone,
                        "address": res.data.address
                    })
                } else {
                    console.log("登录失败");
                }
            },
            error: (err) => {
                window.location = '404.html';
            }
        })

    });
</script>

</body>
</html>
